package com.redlion.object;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.modifier.ease.EaseLinear;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;

public class Planet {

	public static final float ROTATION_DURATION = 20;
	public Sprite sprite;
	public Body body;
	public float centerX;
	public float centerY;
	public float raduis;
	public Vector2 center = new Vector2();
	public IEntityModifier rotation;

	public Planet(float x, float y) {
		this.centerX = x;
		this.centerY = y;
		this.center.set(x, y);
	}

	public void createSprite(ITextureRegion textureRegion) {
		sprite = new Sprite(centerX - (textureRegion.getWidth() / 2), centerY
				- (textureRegion.getHeight() / 2), textureRegion,
				Game.gameActivity.getVertexBufferObjectManager());
		this.raduis = sprite.getWidth() / 2;
	}

	public void createBody(PhysicsWorld physicsWorld, FixtureDef fixtureDef) {
		body = PhysicsFactory.createCircleBody(physicsWorld, sprite,
				BodyType.StaticBody, fixtureDef);
		body.setBullet(false);
		body.setUserData(getClass());
	}

	public void enableRotation(float duration) {
		rotation = new RotationModifier(duration, 0, 360,
				EaseLinear.getInstance()) {
			@Override
			protected void onSetValue(final IEntity pEntity,
					final float pPercentageDone, final float pRotation) {
				if (pPercentageDone == 1) {
					this.reset();
				}
				super.onSetValue(pEntity, pPercentageDone, pRotation);
			}
		};
		sprite.registerEntityModifier(rotation);
	}
}
